package saeildamil.arralist;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.swing.JOptionPane;

import saeildamil.common.Manager;
import saeildamil.common.Student;

public class ArrayListStdMagn implements Manager{
	ArrayList<Student>stdList;
	File file;
	public ArrayListStdMagn(){
		file=new File("Student.dat");
		stdList=new ArrayList<>();
		init();
	}
	
	@Override
	public void init() {
		if(!file.exists()){
			stdList=new ArrayList<>();
		}else{
			inputfile();
		}
		showMenu();
		
	}

	private void showMenu() {
		int num=0;
		String message="1.입력  2.삭제  3.수정  4.검색  5.출력  6.종료";
		do {
			num=Integer.parseInt(JOptionPane.showInputDialog(message).trim());
			switch (num) {
			case 1:
				insertStd();
				break;
			case 2:
				deleteStd();
				break;
			case 3:
				updateStd();
				break;
			case 4:
				searchStd();
				break;
			case 5:
				printList();
				break;
			default:
				outputfile();
				break;
			}
			
		} while (num!=6);
		
	}
	@Override
	public void insertStd() {
		Student std=inputStdinfo();
		String message ="학번:"+std.getStdNum()+" 이름:"+std.getStdName() + " 국어:" + std.getKorScore()
				+ " 수학:" + std.getMathScore() + " 영어:" + std.getEngScore();
		int result = JOptionPane.showConfirmDialog(null, message + "입력하시겠습까");
		   if(result == 0){
		    stdList.add(std);
		    JOptionPane.showMessageDialog(null, "학생정보 입력 완료");
		    System.out.println("학생정보 입력 완료");
		   }else{
		    JOptionPane.showMessageDialog(null, "학생정보 입력이 취소되었습니다.");
		   }
	}
	

	private Student inputStdinfo() {
		String message = "학생 정보를 입력하세요 [학번,성명,국어,영어,수학]";
		String stdInfo = JOptionPane.showInputDialog(message).trim();
		StringTokenizer st = new StringTokenizer(stdInfo, ",");
		String num=st.nextToken();
		String name = st.nextToken();
		int kor = Integer.parseInt(st.nextToken());
		int math = Integer.parseInt(st.nextToken());
		int eng = Integer.parseInt(st.nextToken());
		return new Student(num, name, kor, math, eng);
	}

	@Override
	public void deleteStd() {
		printList(); 
		int num = Integer.parseInt(JOptionPane.showInputDialog("삭제할 학생 번호 입력").trim());
		if(num>stdList.size()){
		System.out.println("삭제할 학생이 없음");
		return;
		}
		stdList.remove(num-1);
		printList();
		System.out.println("삭제완료");
	}

	@Override
	public void updateStd() {
		printList();
		int num = Integer.parseInt(JOptionPane.showInputDialog("수정할 학생 번호를 입력").trim());
		if(num>stdList.size()){
		JOptionPane.showMessageDialog(null, "해당 학생이 존재하지 않음");
		return;
		}
		Student updStd = stdList.get(num-1);
		int subject = Integer.parseInt(JOptionPane.showInputDialog("수정할 과목 번호 입력 1:국어 2:수학 3:영어").trim());
		int jumsu = Integer.parseInt(JOptionPane.showInputDialog("수정할 과목의 점수를 입력").trim());
		switch (subject) {
		case 1:
		updStd.setKorScore(jumsu);
		break;
		case 2:
		updStd.setEngScore(jumsu);
		break;
		case 3:
		updStd.setMathScore(jumsu);
		break;
		default:
		JOptionPane.showMessageDialog(null, "해당 과목이 존재하지 않음");
		System.out.println("해당 과목이 존재하지 않음");
		break;
		}
		printList();
		System.out.println("수정완료");
	}

	@Override
	public Object searchStd() {
		int num = Integer.parseInt(JOptionPane.showInputDialog("검색할 학생 번호 입력").trim());
		if(num>stdList.size()){
		JOptionPane.showMessageDialog(null, "해당 학생이 존재하지 않음");
		return null;
		}else{
		Student std = stdList.get(num-1);
		StringBuilder sb = new StringBuilder();
		sb.append("학번: " + std.getStdNum() + "\n");
		sb.append("성명: " + std.getStdName() + "\n" );
		sb.append("국어: " + std.getKorScore() + "\n");
		sb.append("영어: " + std.getEngScore() + "\n");
		sb.append("수학: " + std.getMathScore() + "\n");
		JOptionPane.showMessageDialog(null, sb);
		}
		return null;
		
	}

	@Override
	public void printList() {
		for(int i=0; i<stdList.size(); i++){
			System.out.println(i+1+" ");
			System.out.println(stdList.get(i));
		}
		for(Student std:stdList)
			System.out.println(std);
		
	}

	@Override
	public void outputfile() {
		ObjectOutputStream oos=null;
		try {
			oos=new ObjectOutputStream(new FileOutputStream(file));
			for(int i=0; i<stdList.size(); i++){
				oos.writeObject(stdList.get(i));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}

	@Override
	public void inputfile() {
		ObjectInputStream ois=null;
		Student std=null;
		try {
			ois=new ObjectInputStream(new FileInputStream(file));
			while(true){
				std=(Student) ois.readObject();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
